local Msg = require("core.Msg")
local LoginConfig = require("excel.Fuli").login
local FuliDefine = require("fuli.FuliDefine")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local sbConfig = require("excel.Shenbing").shenbing
local RoleAttr = require("role.RoleAttr")
local ShenbingLogic = require("shenbing.ShenbingLogic")
local Handler = require("fuli.HandlerL")
local Log = require("common.Log")

--发送七天登录奖励神兵信息
function sendLoginSbInfo(human)
  local config = sbConfig[5]
  local sendMsg = Msg.gc.GC_SHENBING_INFO
  sendMsg.id = 5
  sendMsg.type = config.type
  sendMsg.awake = config.awake
  local attr = ShenbingLogic.getAttr(5, 0, 0, config.initStar)
  sendMsg.zhandouli = RoleAttr.calHumanPower(attr)
  sendMsg.bodyId = config.bodyId[1]
  Msg.send(sendMsg, human.fd)
end
--检查是否可领取奖励
function checkShowTips(human)
  if human.db.loginChange == 1 then
    LoginConfig = require("excel.Fuli").keepLogin
  else
    LoginConfig = require("excel.Fuli").login
  end
  for i=1, #LoginConfig do
    if human.db.fuliIsGet[FuliDefine.FULI_TYPE_LOGIN * 15 + i - 1] == FuliDefine.REWARD_CAN_GET then
      return 1
    end
  end
  return 0
end
--检查奖励是否领取完
function checkGetAll(human)
  if human.db.loginChange == 1 then
    LoginConfig = require("excel.Fuli").keepLogin
  else
    LoginConfig = require("excel.Fuli").login
  end
  for i=1, #LoginConfig do
    if human.db.fuliIsGet[FuliDefine.FULI_TYPE_LOGIN * 15 + i - 1] ~= FuliDefine.REWARD_ALREADY_GET then
      return false
    end
  end
  return true
end
--发送登录了多少天
function sendLoginDay(human)
  local sendMsg = Msg.gc.GC_FULI_LOGIN_DAY
  sendMsg.days = human.db.loginKeepDay
  Msg.send(sendMsg, human.fd)
end
--奖励列表
function getRewardList(human)
  local sendMsg = Msg.gc.GC_FULI_REWARD
  local cache = {}
  if human.db.loginChange == 1 then
    LoginConfig = require("excel.Fuli").keepLogin
  else
    LoginConfig = require("excel.Fuli").login
  end
  sendMsg.list[0] = #LoginConfig
  for i=1, #LoginConfig do
    sendMsg.list[i].page = FuliDefine.FULI_TYPE_LOGIN
    sendMsg.list[i].canGet = human.db.fuliIsGet[FuliDefine.FULI_TYPE_LOGIN * 15 + i - 1]
    sendMsg.list[i].vipCanGet = human.db.loginChange
    sendMsg.list[i].condition = LoginConfig[i].index
    sendMsg.list[i].needYuanbao = 0
    sendMsg.list[i].reward[0] = #LoginConfig[i].reward
    for j=1, #LoginConfig[i].reward do
      Grid.create(cache, LoginConfig[i].reward[j][1], LoginConfig[i].reward[j][2])
      Grid.makeItem(cache, sendMsg.list[i].reward[j], 0, human)
    end    
    sendMsg.list[i].vipReward[0] = 0 
    sendMsg.list[i].onlineTime = 0
    sendMsg.list[i].percent = 0
  end
  Msg.send(sendMsg, human.fd)
  sendLoginDay(human)
end

function getIndex(human, condition)
  if human.db.loginChange == 1 then
    LoginConfig = require("excel.Fuli").keepLogin
  else
    LoginConfig = require("excel.Fuli").login
  end
  for i=1, #LoginConfig do
    if LoginConfig[i].index == condition then
      return i
    end
  end
  return 0
end

function getReward(human, condition)
  local index = getIndex(human, condition)
  if index == 0 then
    return
  end
  if human.db.fuliIsGet[FuliDefine.FULI_TYPE_LOGIN * 15 + index - 1] ~= FuliDefine.REWARD_CAN_GET then
    return
  end
  if human.db.loginChange == 1 then
    LoginConfig = require("excel.Fuli").keepLogin
  else
    LoginConfig = require("excel.Fuli").login
  end
  if not BagLogic.checkCanAddItemList(human, LoginConfig[index].reward,#LoginConfig[index].reward) then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.FULI_BAG_NOT_ENOUGH)
  end
  human.db.fuliIsGet[FuliDefine.FULI_TYPE_LOGIN * 15 + index - 1] = FuliDefine.REWARD_ALREADY_GET
  for i=1, #LoginConfig[index].reward do
    BagLogic.addItem(human, LoginConfig[index].reward[i][1], 1, LoginConfig[index].reward[i][2], "fulidating")
  end  
  getRewardList(human)
  Handler.checkShowTips(human)
  Log.write(Log.LOGID_OSS_DAILY_LOGIN, human._id, human.account, human.name,human.db.lv, human.db.loginChange, condition)
end

function updateStatus(human)
  if human.db.loginChange == 1 then
    LoginConfig = require("excel.Fuli").keepLogin
  else
    LoginConfig = require("excel.Fuli").login
  end
  if human.db.fuliIsGet[FuliDefine.FULI_TYPE_LOGIN * 15 + #LoginConfig - 1] == FuliDefine.REWARD_ALREADY_GET then
    human.db.loginKeepDay = 1
    for i=1, #LoginConfig do
      human.db.fuliIsGet[FuliDefine.FULI_TYPE_LOGIN * 15 + i - 1] = FuliDefine.REWARD_NOT_CAN_GET
    end
    human.db.fuliIsGet[FuliDefine.FULI_TYPE_LOGIN * 15 + 1 - 1] = FuliDefine.REWARD_CAN_GET
    if human.db.loginChange == 0 then
      human.db.loginChange = 1
    end
  end
  for i=1, #LoginConfig do
    if human.db.loginKeepDay >= LoginConfig[i].index 
    and human.db.fuliIsGet[FuliDefine.FULI_TYPE_LOGIN * 15 + i - 1] == FuliDefine.REWARD_NOT_CAN_GET then
      human.db.fuliIsGet[FuliDefine.FULI_TYPE_LOGIN * 15 + i - 1] = FuliDefine.REWARD_CAN_GET
    end
  end
end












